package com.oliet.common.vo.role;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.common.collect.Lists;
import com.oliet.common.vo.permission.PermissionVO;
import com.oliet.entity.Roles;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jiaZhenpeng
 * @Title: RoleVO
 * @Package com.oliet.common.vo.role
 * @Description: 角色返回数据
 * @date 2023-12-03 16:07
 */
@Data
public class RoleVO {

    /**
     * id
     */
    private Long id;

    /**
     * 编码
     */
    private String code;

    /**
     * 角色名称
     */
    private String roleName;

    /**
     * 角色类型
     */
    private String roleType;

    /**
     * 描述
     */
    private String description;

    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date dateCreated;

    /**
     * 角色权限
     */
    private List<Long> permissionIds;

    /**
     * 权限列表
     */
    private List<PermissionVO> permissionVoList;

    public static List<RoleVO> poListConverVoList(List<Roles> poList, Map<Long, List<Long>> permissionIdMap) {
        List<RoleVO> result = Lists.newArrayList();
        if (CollectionUtil.isEmpty(poList)){
            return result;
        }
        result = poList.stream().map(po->{
            RoleVO vo = new RoleVO();
            BeanUtils.copyProperties(po, vo);
            vo.setPermissionIds(permissionIdMap.get(vo.getId()));
            return vo;
        }).collect(Collectors.toList());
        return result;
    }

    public static RoleVO poConverVo(Roles po) {
        RoleVO vo = new RoleVO();
        if (ObjectUtil.isNotNull(po)){
            BeanUtil.copyProperties(po,vo);
        }
        return vo;
    }
}
